#-*-coding:utf-8-*-
# Create your views here.
from crm.models import Members
from crm.models import Salesperson
from crm.models import Agent
from crm.models import Furtherinterest
from crm.models import Projects
from django.shortcuts import get_object_or_404,render_to_response
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.core.paginator import Paginator, InvalidPage

"""按Agent统计"""
def agent(request):
    if request.session['level']!="administrators":
        return HttpResponseRedirect('/')
    project_list = Projects.objects.order_by('id')
    if request.method == 'GET':        
        return render_to_response('sta_agent.html',{'project_list':project_list})#显示初始页
    else:#显示统计结果
        start_date=request.POST.get('start_date','begin')
        end_date=request.POST.get('end_date','now')
        p_id=request.POST.get('p_id','0')
        agent=Agent.objects.all()
        if start_date == 'begin' and end_date =='now':            
            members=Members.objects.all()
        elif start_date =='begin' and end_date != 'now':
            members=Members.objects.filter(regtime__lte=end_date)
        elif start_date != 'begin' and end_date =='now':
            members=Members.objects.filter(regtime__gte=start_date)
        else:
            members=Members.objects.filter(regtime__range=(start_date, end_date))
        if p_id !='0':
            members=members.filter(projects__pk=int(p_id))

        total=members.count()#指定时间内会员总数

        for a in agent:
            budget=a.budget#该Agent预算
            a.member=members.filter(agent__id=a.id).count()#该agent总人数
            member=float(a.member)
            email=a.members_set.filter(received_emaill=1).count()#收到emai人数
            hardcopy=members.filter(agent__id=a.id,received_hardcopy=1).count()#收到hardcopy人数
            i_0=members.filter(agent__id=a.id,interlevel=0).count()#兴趣为0的人数
            i_1=members.filter(agent__id=a.id,interlevel=1).count()#兴趣为1的人数
            i_2=members.filter(agent__id=a.id,interlevel=2).count()#兴趣为2的人数
            i_3=members.filter(agent__id=a.id,interlevel=3).count()#兴趣为3的人数
            i_4=members.filter(agent__id=a.id,interlevel=4).count()#兴趣为4的人数
            s_0=members.filter(agent__id=a.id,status=0).count()#跟进为0的人数
            s_1=members.filter(agent__id=a.id,status=1).count()#跟进为1的人数
            s_2=members.filter(agent__id=a.id,status=2).count()#跟进为2的人数
            s_3=members.filter(agent__id=a.id,status=3).count()#跟进为3的人数
            s_4=members.filter(agent__id=a.id,status=4).count()#跟进为4的人数
            s_5=members.filter(agent__id=a.id,status=5).count()#跟进为5的人数
            s_6=members.filter(agent__id=a.id,status=6).count()#跟进为6的人数
            s_7=members.filter(agent__id=a.id,status=7).count()#跟进为7的人数
            if member==0:#会员数为0时
                a.pmembers='0' + '%'
                a.pcost=a.pbudget=a.pemail=a.phardcopy=a.pi_0=a.pi_1=a.pi_2=a.pi_3=a.pi_4=a.ps_0=a.ps_1=a.ps_2=a.ps_3=a.ps_4=a.ps_5=a.ps_6=a.ps_7='--'
            else:
                a.pmembers='%0.2f' % (member/total*100)+ '%'
                if budget==0:#预算为0时
                    a.pbudget='0'#填表成本为0
                    a.pcost='0'#成为学员成本为0
                else:
                    a.pcost='%0.2f' % (budget/member)#填表成本
                    if s_7==0:
                        a.pbudget='--'
                    else:
                        a.pbudget='%0.2f' % (budget/s_7)#成为学员成本
                if email==0:
                    a.pemail='0' + '%'
                else:
                    a.pemail='%0.2f' % (email/member*100)+ '%'#收到email的人数比例
                if hardcopy==0:
                    a.phardcopy='0' + '%'
                else:
                    a.phardcopy='%0.2f' % (hardcopy/member*100)+ '%'#收到hardcopy的人数比例
                if i_0 ==0:
                    a.pi_0='0' + '%'
                else:
                    a.pi_0='%0.2f' % (i_0/member*100)+ '%'#interestlevl为Doesn't cantact yet 的人数比例
                if i_1 ==0:
                    a.pi_1='0' + '%'
                else:
                    a.pi_1='%0.2f' % (i_1/member*100)+ '%'#interestlevl为Hign interest的人数比例
                if i_2 ==0:
                    a.pi_2='0' + '%'
                else:
                    a.pi_2='%0.2f' % (i_2/member*100)+ '%'#interestlevl为some interest的人数比例
                if i_3 ==0:
                    a.pi_3='0' + '%'
                else:
                    a.pi_3='%0.2f' % (i_3/member*100)+ '%'#interestlevl为 no interest的人数比例
                if i_4 ==0:
                    a.pi_4='0' + '%'
                else:
                    a.pi_4='%0.2f' % (i_4/member*100)+ '%'#interestlevl为 consider next time的人数比例
                if s_0 ==0:
                    a.ps_0='0' + '%'
                else:
                    a.ps_0='%0.2f' % (s_0/member*100)+ '%'#follow_up status为Doesnt.cantact yet的人数比例
                if s_1 ==0:
                    a.ps_1='0' + '%'
                else:
                    a.ps_1='%0.2f' % (s_1/member*100)+ '%'#follow_up status为cannot be reached的人数比例
                if s_2 ==0:
                    a.ps_2='0' + '%'
                else:
                    a.ps_2='%0.2f' % (s_2/member*100)+ '%'#follow_up status为Appointment scheduled的人数比例
                if s_3 ==0:
                    a.ps_3='0' + '%'
                else:
                    a.ps_3='%0.2f' % (s_3/member*100)+ '%'#follow_up status为Under consideration的人数比例
                if s_4 ==0:
                    a.ps_4='0' + '%'
                else:
                    a.ps_4='%0.2f' % (s_4/member*100)+ '%'#follow_up status为Please call later的人数比例
                if s_5 ==0:
                    a.ps_5='0' + '%'
                else:
                    a.ps_5='%0.2f' % (s_5/member*100)+ '%'#follow_up status为Schedule conflict/next time的人数比例
                if s_6 ==0:
                    a.ps_6='0' + '%'
                else:
                    a.ps_6='%0.2f' % (s_6/member*100)+ '%'#follow_up status为 Don't call again的人数比例
                if s_7 ==0:
                    a.ps_7='0' + '%'
                else:
                    a.ps_7='%0.2f' % (s_7/member*100)+ '%'#follow_up status为Enrolled的人数比例
            
        return render_to_response('sta_agent.html',{'project_list':project_list,'agent':agent,'start_date':start_date,'end_date':end_date,'p_id':int(p_id)})

"""按Sales统计"""
def sales(request):
    if request.session['level']!="administrators":
        return HttpResponseRedirect('/')
    project_list = Projects.objects.order_by('id')
    if request.method == 'GET':
        project_list = Projects.objects.order_by('id')
        return render_to_response('sta_sales.html',{'project_list':project_list})
    else:#显示统计结果
        start_date=request.POST.get('start_date','begin')
        end_date=request.POST.get('end_date','now')
        p_id=request.POST.get('p_id','0')
        sales=Salesperson.objects.all()
        if start_date == 'begin' and end_date =='now':            
            members=Members.objects.all()
        elif start_date =='begin' and end_date != 'now':
            members=Members.objects.filter(regtime__lte=end_date)
        elif start_date != 'begin' and end_date =='now':
            members=Members.objects.filter(regtime__gte=start_date)
        else:
            members=Members.objects.filter(regtime__range=(start_date, end_date))
        if p_id !='0':
            members=members.filter(projects__pk=int(p_id))

        total=members.count()#指定时间内会员总数

        for a in sales:
            a.member=members.filter(salesperson__id=a.id).count()#该sales总人数
            member=float(a.member)
            a.all=int(member)
            email=a.members_set.filter(received_emaill=1).count()#收到emai人数
            hardcopy=members.filter(salesperson__id=a.id,received_hardcopy=1).count()#收到hardcopy人数
            i_0=members.filter(salesperson__id=a.id,interlevel=0).count()#兴趣为0的人数
            i_1=members.filter(salesperson__id=a.id,interlevel=1).count()#兴趣为1的人数
            i_2=members.filter(salesperson__id=a.id,interlevel=2).count()#兴趣为2的人数
            i_3=members.filter(salesperson__id=a.id,interlevel=3).count()#兴趣为3的人数
            i_4=members.filter(salesperson__id=a.id,interlevel=4).count()#兴趣为4的人数
            s_0=members.filter(salesperson__id=a.id,status=0).count()#跟进为0的人数
            s_1=members.filter(salesperson__id=a.id,status=1).count()#跟进为1的人数
            s_2=members.filter(salesperson__id=a.id,status=2).count()#跟进为2的人数
            s_3=members.filter(salesperson__id=a.id,status=3).count()#跟进为3的人数
            s_4=members.filter(salesperson__id=a.id,status=4).count()#跟进为4的人数
            s_5=members.filter(salesperson__id=a.id,status=5).count()#跟进为5的人数
            s_6=members.filter(salesperson__id=a.id,status=6).count()#跟进为6的人数
            s_7=members.filter(salesperson__id=a.id,status=7).count()#跟进为7的人数
            s_e=members.filter(salesperson__id=a.id,status=7,last_allocate__isnull=True).count()#跟进为7并未转过其他人的人数
            s_c=s_7-s_e#非一次跟踪
            a.i_o=i_0
            a.i_1=i_1
            a.i_2=i_2
            a.i_3=i_3
            a.i_4=i_4
            a.s_0=s_0
            a.s_1=s_1
            a.s_2=s_2
            a.s_3=s_3
            a.s_4=s_4
            a.s_5=s_5
            a.s_6=s_6
            a.s_7=s_7
            a.s_e=s_e
            a.s_c=s_c
            if member==0:
                a.pmembers='0' + '%'
                a.pemail=a.phardcopy=a.pi_0=a.pi_1=a.pi_2=a.pi_3=a.pi_4=a.ps_0=a.ps_1=a.ps_2=a.ps_3=a.ps_4=a.ps_5=a.ps_6=a.ps_7='--'
            else:
                a.pmembers='%0.2f' % (member/total*100) + '%'
                if email==0:
                    a.pemail='0' + '%'
                else:
                    a.pemail='%0.2f' % (email/member*100)+ '%'#收到email的人数比例
                if hardcopy==0:
                    a.phardcopy='0' + '%'
                else:
                    a.phardcopy='%0.2f' % (hardcopy/member*100)+ '%'#收到hardcopy的人数比例
                if i_0 ==0:
                    a.pi_0='0' + '%'
                else:
                    a.pi_0='%0.2f' % (i_0/member*100)+ '%'#interestlevl为Doesn't cantact yet 的人数比例
                if i_1 ==0:
                    a.pi_1='0' + '%'
                else:
                    a.pi_1='%0.2f' % (i_1/member*100)+ '%'#interestlevl为Hign interest的人数比例
                if i_2 ==0:
                    a.pi_2='0' + '%'
                else:
                    a.pi_2='%0.2f' % (i_2/member*100)+ '%'#interestlevl为some interest的人数比例
                if i_3 ==0:
                    a.pi_3='0' + '%'
                else:
                    a.pi_3='%0.2f' % (i_3/member*100)+ '%'#interestlevl为 no interest的人数比例
                if i_4 ==0:
                    a.pi_4='0' + '%'
                else:
                    a.pi_4='%0.2f' % (i_4/member*100)+ '%'#interestlevl为 consider next time的人数比例
                if s_0 ==0:
                    a.ps_0='0' + '%'
                else:
                    a.ps_0='%0.2f' % (s_0/member*100)+ '%'#follow_up status为Doesnt.cantact yet的人数比例
                if s_1 ==0:
                    a.ps_1='0' + '%'
                else:
                    a.ps_1='%0.2f' % (s_1/member*100)+ '%'#follow_up status为cannot be reached的人数比例
                if s_2 ==0:
                    a.ps_2='0' + '%'
                else:
                    a.ps_2='%0.2f' % (s_2/member*100)+ '%'#follow_up status为Appointment scheduled的人数比例
                if s_3 ==0:
                    a.ps_3='0' + '%'
                else:
                    a.ps_3='%0.2f' % (s_3/member*100)+ '%'#follow_up status为Under consideration的人数比例
                if s_4 ==0:
                    a.ps_4='0' + '%'
                else:
                    a.ps_4='%0.2f' % (s_4/member*100)+ '%'#follow_up status为Please call later的人数比例
                if s_5 ==0:
                    a.ps_5='0' + '%'
                else:
                    a.ps_5='%0.2f' % (s_5/member*100)+ '%'#follow_up status为Schedule conflict/next time的人数比例
                if s_6 ==0:
                    a.ps_6='0' + '%'
                else:
                    a.ps_6='%0.2f' % (s_6/member*100)+ '%'#follow_up status为 Don't call again的人数比例
                if s_7 ==0:
                    a.ps_7='0' + '%'
                else:
                    a.ps_7='%0.2f' % (s_7/member*100)+ '%'#follow_up status为Enrolled的人数比例

                if s_e ==0:
                    a.ps_e='0' + '%'
                else:
                    a.ps_e='%0.2f' % (s_e/member*100)+ '%'#follow_up status为Enrolled的人数比例(一次跟踪)
                if s_c ==0:
                    a.ps_c='0' + '%'
                else:
                    a.ps_c='%0.2f' % (s_c/member*100)+ '%'#follow_up status为Enrolled的人数比例(非一次跟踪)
        return render_to_response('sta_sales.html',{'project_list':project_list,'sales':sales,'start_date':start_date,'end_date':end_date,'p_id':int(p_id)})
